#include <cstring>
#include <iostream>
#include <algorithm>

#define f(x, l, r) for (int x = l; x <= r; ++ x)

using namespace std;

const int N = 110;

int n, m;
int d[N][N];

int main() {
    cin >> n >> m;
    
    memset(d, 0x3f, sizeof d);
    while (m -- ) {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        d[a][b] = d[b][a] = min(d[a][b], c);
    }
    
    f(k, 1, n) f(i, 1, n) f(j, 1, n)
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
        
    int res = 0;
    f(i, 2, n)
        if (d[1][i] == 0x3f3f3f3f) {
            res = -1;
            break;
        }
        else res = max(res, d[1][i]);
    
    cout << res << endl;
    
    return 0;
}